package com.longshihan.carservice.controller;

import com.github.pagehelper.PageInfo;
import com.longshihan.carservice.http.APIResponse;
import com.longshihan.carservice.model.GoodsDomain;
import com.longshihan.carservice.service.goods.GoodsService;
import com.longshihan.carservice.utils.GsonUtils;
import com.longshihan.carservice.utils.UUID;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * @author longhe
 * @time
 */
@Api("商品相关")
@Controller
@RequestMapping("/goods")
public class GoodsController extends BaseController {


    @Autowired
    private GoodsService goodsService;


    @ApiOperation(value = "添加商品",notes = "json转义添加")
    @PostMapping(value = "/addgoodsbean")
    @ResponseBody
    public APIResponse saveGoods(HttpServletRequest request,  HttpServletResponse response,
                                 @ApiParam(name = "goods", value = "商品", required = true)
                                 @RequestParam(name = "goods", required = true) String goodsJson,
                                 @ApiParam(name = "authorId", value = "用户ID", required = true)
                                     @RequestParam(name = "authorId", required = true) String authorId) {
        try{
            GoodsDomain domain= (GoodsDomain) GsonUtils.JSONToObject(goodsJson,GoodsDomain.class);
            if (domain==null){
                String msg = "商品序列化失败";
                return APIResponse.fail(msg);
            }
            domain.setAuthorId(authorId);
            domain.setGoodsId(UUID.UU64());
            goodsService.addGoodsBean(domain);
            return APIResponse.success();
        }catch (Exception e){
            String msg = "添加商品失败";
            return APIResponse.fail(e.getMessage());
        }
    }

    @ApiOperation(value = "添加商品",notes = "一个一个添加")
    @PostMapping(value = "/addgoods")
    @ResponseBody
    public APIResponse saveGoods(HttpServletRequest request,  HttpServletResponse response,
                                 @ApiParam(name = "goodsName", value = "商品名称", required = true) @RequestParam(name = "goodsName", required = true) String goodsName,
                                 @ApiParam(name = "goodsImage", value = "商品图片") @RequestParam(name = "goodsImage") String goodsImage,
                                 @ApiParam(name = "des", value = "商品描述") @RequestParam(name = "des") String des,
                                 @ApiParam(name = "authorId", value = "用户ID", required = true) @RequestParam(name = "authorId", required = true) String authorId,
                                 @ApiParam(name = "price", value = "商品价格") @RequestParam(name = "price", required = false) double price,
                                 @ApiParam(name = "type", value = "类型") @RequestParam(name = "type") String type,
                                 @ApiParam(name = "status", value = "商品状态") @RequestParam(name = "status") String status,
                                 @ApiParam(name = "tags", value = "商品标签列表") @RequestParam(name = "tags") String tags,
                                 @ApiParam(name = "categories", value = "商品分类列表") @RequestParam(name = "categories") String categories,
                                 @ApiParam(name = "originprice", value = "原始商品价格") @RequestParam(name = "originprice", required = false) double originprice,
                                 @ApiParam(name = "province", value = "商品产地") @RequestParam(name = "province") String province

    ) {
        try{
            goodsService.addGoods(UUID.UU64(),goodsName,des,
                    authorId,type,status,
                    price,originprice,province,goodsImage,tags,categories);
            return APIResponse.success();
        }catch (Exception e){
            String msg = "添加商品失败";
            return APIResponse.fail(e.getMessage());
        }
    }


    @ApiOperation(value = "更新商品",notes = "更新商品")
    @PostMapping(value = "/updategoodsbean")
    @ResponseBody
    public APIResponse saveGoodsBean(HttpServletRequest request,  HttpServletResponse response,
                                 @ApiParam(name = "authorId", value = "用户ID", required = true) @RequestParam(name = "authorId", required = true) String authorId,
                                 @ApiParam(name = "goods", value = "商品", required = true) @RequestParam(name = "goods", required = true) String goods
    ) {
        try{
            GoodsDomain domain= (GoodsDomain) GsonUtils.JSONToObject(goods,GoodsDomain.class);
            if (domain==null){
                String msg = "更新商品序列化失败";
                return APIResponse.fail(msg);
            }
            goodsService.updateGoodsBean(domain);
            return APIResponse.success();
        }catch (Exception e){
            String msg = "更新商品失败";
            return APIResponse.fail(e.getMessage());
        }
    }

    @ApiOperation(value = "更新商品",notes = "更新商品")
    @PostMapping(value = "/updategoods")
    @ResponseBody
    public APIResponse updateGoods(HttpServletRequest request,  HttpServletResponse response,
                                 @ApiParam(name = "goodsId", value = "商品ID", required = true) @RequestParam(name = "goodsId", required = true) String goodsId,
                                 @ApiParam(name = "goodsName", value = "商品名称", required = true) @RequestParam(name = "goodsName", required = true) String goodsName,
                                 @ApiParam(name = "goodsImage", value = "商品图片") @RequestParam(name = "goodsImage") String goodsImage,
                                 @ApiParam(name = "des", value = "商品描述") @RequestParam(name = "des") String des,
                                 @ApiParam(name = "authorId", value = "用户ID", required = true) @RequestParam(name = "authorId", required = true) String authorId,
                                 @ApiParam(name = "price", value = "商品价格") @RequestParam(name = "price", required = false) double price,
                                 @ApiParam(name = "type", value = "类型") @RequestParam(name = "type") String type,
                                 @ApiParam(name = "status", value = "商品状态") @RequestParam(name = "status") String status,
                                 @ApiParam(name = "tags", value = "商品标签列表") @RequestParam(name = "tags") String tags,
                                 @ApiParam(name = "categories", value = "商品分类列表") @RequestParam(name = "categories") String categories,
                                 @ApiParam(name = "originprice", value = "原始商品价格") @RequestParam(name = "originprice", required = false) double originprice,
                                 @ApiParam(name = "province", value = "商品产地") @RequestParam(name = "province") String province

    ) {
        try{
            goodsService.updateGoods(authorId,goodsId,goodsName,des,type,status,price,originprice,province,goodsImage,tags,categories);
            return APIResponse.success();
        }catch (Exception e){
            String msg = "更新商品失败";
            return APIResponse.fail(e.getMessage());
        }
    }



    @ApiOperation(value = "删除商品",notes = "删除商品")
    @PostMapping(value = "/delGoodsId")
    @ResponseBody
    public APIResponse delGoodsById(HttpServletRequest request,  HttpServletResponse response,
                                     @ApiParam(name = "authorId", value = "用户ID", required = true) @RequestParam(name = "authorId", required = true) String authorId,
                                     @ApiParam(name = "goodsId", value = "商品ID", required = true) @RequestParam(name = "goodsId", required = true) String goodsId
    ) {
        try{
            goodsService.deleteGoodsById(goodsId);
            return APIResponse.success();
        }catch (Exception e){
            String msg = "删除商品失败";
            return APIResponse.fail(e.getMessage());
        }
    }

    @ApiOperation(value = "查询单个商品",notes = "查询单个商品")
    @PostMapping(value = "/getGoods")
    @ResponseBody
    public APIResponse getGoodsById(HttpServletRequest request,  HttpServletResponse response,
                                    @ApiParam(name = "authorId", value = "用户ID", required = true) @RequestParam(name = "authorId", required = true) String authorId,
                                    @ApiParam(name = "goodsId", value = "商品ID", required = true) @RequestParam(name = "goodsId", required = true) String goodsId
    ) {
        try{
           GoodsDomain goodsDomain= goodsService.getGoodsById(goodsId,authorId);
           if (goodsDomain!=null){
               return APIResponse.success(GsonUtils.toJsonString(goodsDomain));
           }else {
               return APIResponse.fail("获取不到商品信息");
           }

        }catch (Exception e){
            String msg = "查询商品失败";
            return APIResponse.fail(e.getMessage());
        }
    }

    @ApiOperation(value = "查询全部商品",notes = "查询单个用户上架的全部商品")
    @PostMapping(value = "/getAllAuthorGoods")
    @ResponseBody
    public APIResponse getAllGoodsByUserId(HttpServletRequest request,  HttpServletResponse response,
                                    @ApiParam(name = "authorId", value = "用户ID", required = true) @RequestParam(name = "authorId", required = true) String authorId,
                                    @ApiParam(name = "pageNum", value = "页面数",defaultValue = "20") @RequestParam(name = "pageNum",defaultValue = "20") int pageNum,
                                    @ApiParam(name = "pagesize", value = "单次查询的个数", required = true) @RequestParam(name = "pagesize", required = true) int pagesize
    ) {
        try{
            PageInfo<GoodsDomain> goodsDomains=goodsService.getAuthorGoods(authorId,pageNum,pagesize);
            if (goodsDomains!=null){
                return APIResponse.success(GsonUtils.toJsonString(goodsDomains));
            }else {
                return APIResponse.fail("获取不到商品信息");
            }

        }catch (Exception e){
            String msg = "查询商品失败";
            return APIResponse.fail(e.getMessage());
        }
    }

    @ApiOperation(value = "查询全部商品",notes = "查询单个用户上架的全部商品")
    @PostMapping(value = "/getAllGoods")
    @ResponseBody
    public APIResponse getAllGoods(HttpServletRequest request,  HttpServletResponse response,
                                           @ApiParam(name = "pageNum", value = "页面数", required = true) @RequestParam(name = "pageNum", required = true) int pageNum,
                                           @ApiParam(name = "pagesize", value = "单次查询的个数", required = true) @RequestParam(name = "pagesize", required = true) int pagesize
    ) {
        try{
            PageInfo<GoodsDomain> goodsDomains=goodsService.getGoods(pageNum,pagesize);
            if (goodsDomains!=null){
                return APIResponse.success(GsonUtils.toJsonString(goodsDomains));
            }else {
                return APIResponse.fail("获取不到商品信息");
            }

        }catch (Exception e){
            String msg = "删除商品失败";
            return APIResponse.fail(e.getMessage());
        }
    }

}
